package zmwxapplet

import (
	"github.com/gin-gonic/gin"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/devices"
	"yundian/internel/app/models/liveservicelog"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

// HandleInsertLiveServiceLog 新增服务日志
func HandleInsertLiveServiceLog(ctx *gin.Context) {
	userId, _ := helper.GetUserIdFromCtx(ctx)
	openid, _ := helper.GetUserNameFromCtx(ctx)

	param := &request.LiveServiceInsertReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	m := &liveservicelog.LiveServiceLogModel{}
	//获取设备信息
	devInfo := dao.NewDeviceDao().GetDeviceInfoWithPlace(param.DeviceSn).Unwrap().(*devices.DeviceWithPlace)
	m.PlaceId = devInfo.PlaceId
	m.PlaceName = devInfo.PlaceName
	m.ClientIp = ctx.ClientIP()
	m.CreateAt = dingo.Time(time.Now().Unix())
	m.DeviceSn = param.DeviceSn
	m.Openid = openid
	m.MemberId = userId
	m.Appid = param.AppId
	m.ServiceType = param.ServiceType
	m.ServiceDesc = param.ServiceDesc

	Response(ctx)("", CodeInsertSuccess, dao.NewLiveServiceLogDao().InsertLiveServiceLog(m).Unwrap())(OK)
}

